//
//  ViewController.m
//  CAShapeLayer
//
//  Created by Xwoder on 2017/2/4.
//  Copyright © 2017年 Xwoder. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, weak) CAShapeLayer *shapeLayer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    CGRect rect = CGRectMake(0, 0, 300, 300);
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:rect];
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.bounds = rect;
    shapeLayer.anchorPoint = CGPointMake(0.5, 0.5);
    shapeLayer.position = self.view.center;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = [UIColor orangeColor].CGColor;
    shapeLayer.strokeEnd = 0;
    shapeLayer.lineWidth = 5;
    shapeLayer.lineCap = kCALineCapRound;
    shapeLayer.path = bezierPath.CGPath;
    
    [self.view.layer addSublayer:shapeLayer];
    
    CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateCircle:)];
    displayLink.frameInterval = 5;
    [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    
    self.shapeLayer = shapeLayer;
}

- (void)updateCircle:(CADisplayLink *)displayLink {
    if (self.shapeLayer.strokeEnd >= 1) {
        [displayLink invalidate];
        return;
    }
    self.shapeLayer.strokeEnd += 0.1;
}

@end
